package com.mnyc.research.service.impl;

import java.util.List;
import java.util.concurrent.SynchronousQueue;

import cn.hutool.core.util.StrUtil;
import com.mnyc.common.utils.DateUtils;
import com.mnyc.common.utils.SecurityUtils;
import com.mnyc.research.service.IJyActivityTeacherService;
import com.mnyc.system.enmu.research.ActivityUserTypeEnum;
import com.mnyc.system.enmu.system.ProcessStatusEnum;
import com.mnyc.system.service.ISysFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mnyc.research.mapper.JySchoolResearchActivityMapper;
import com.mnyc.research.domain.JySchoolResearchActivity;
import com.mnyc.research.service.IJySchoolResearchActivityService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * 校本教研活动Service业务层处理
 *
 * @author wdy
 * @date 2025-08-08
 */
@Service
public class JySchoolResearchActivityServiceImpl implements IJySchoolResearchActivityService {
    @Autowired
    private JySchoolResearchActivityMapper mapper;
    @Autowired
    private ISysFileInfoService fileInfoService;
    @Autowired
    private IJyActivityTeacherService teacherService;

    private final String BUSINESS_TYPE = "jy_school_research_activity";
    private final String CONTENT_SUBTYPE = "content";
    private final String IMG_SUBTYPE = "img";

    /**
     * 查询 校本教研活动
     *
     * @param id 校本教研活动主键
     * @return 校本教研活动
     */
    @Override
    public JySchoolResearchActivity selectJySchoolResearchActivityById(Long id) {
        JySchoolResearchActivity activity = mapper.selectJySchoolResearchActivityById(id);

        activity.setAbsenceUserList(teacherService.getActivityUser(activity.getId(), BUSINESS_TYPE, ActivityUserTypeEnum.ABSENCE.getCode()));
        activity.setLateUserList(teacherService.getActivityUser(activity.getId(), BUSINESS_TYPE, ActivityUserTypeEnum.LATE.getCode()));
        activity.setLeaderUserList(teacherService.getActivityUser(activity.getId(), BUSINESS_TYPE, ActivityUserTypeEnum.LEAVE.getCode()));
        return activity;
    }

    /**
     * 查询 校本教研活动列表
     *
     * @param activity 校本教研活动
     * @return 校本教研活动
     */
    @Override
    public List<JySchoolResearchActivity> selectJySchoolResearchActivityList(JySchoolResearchActivity activity) {
        return mapper.selectJySchoolResearchActivityList(activity);
    }

    /**
     * 新增 校本教研活动
     *
     * @param activity 校本教研活动
     * @return 结果
     */

    @Transactional
    @Override
    synchronized public int insertJySchoolResearchActivity(JySchoolResearchActivity activity) {
        activity.setCreateBy(SecurityUtils.getUsername());
        activity.setCreateTime(DateUtils.getNowDate());
        activity.setAuditState(ProcessStatusEnum.AUDIT.getCode());

        mapper.insertJySchoolResearchActivity(activity);

        saveFileAndTeacherRela(activity);
        return 1;
    }

    /**
     * 修改 校本教研活动
     *
     * @param activity 校本教研活动
     * @return 结果
     */

    @Transactional
    @Override
    synchronized public int updateJySchoolResearchActivity(JySchoolResearchActivity activity) {
        JySchoolResearchActivity dbActivity = mapper.selectJySchoolResearchActivityById(activity.getId());
        Assert.isTrue(ProcessStatusEnum.REJECT.isEqualsTo(dbActivity.getAuditState()), "当前状态不能修改");

        activity.setUpdateBy(SecurityUtils.getUsername());
        activity.setUpdateTime(DateUtils.getNowDate());
        activity.setAuditState(ProcessStatusEnum.AUDIT.getCode());

        saveFileAndTeacherRela(activity);
        return mapper.updateJySchoolResearchActivity(activity);
    }

    /**
     * 删除 校本教研活动信息
     *
     * @param id 校本教研活动主键
     * @return 结果
     */

    @Transactional
    @Override
    synchronized public int deleteJySchoolResearchActivityById(Long id) {
        JySchoolResearchActivity dbActivity = selectJySchoolResearchActivityById(id);
        Assert.isTrue(SecurityUtils.getUsername().equals(dbActivity.getCreateBy()), "只能删除自己创建的活动");
        Assert.isTrue(ProcessStatusEnum.canDelete(dbActivity.getAuditState()), "当前状态不能删除");
        return mapper.deleteJySchoolResearchActivityById(id);
    }

    /**
     * 审核 校本教研活动信息
     *
     * @param activity 校本教研活动主键
     * @return 结果
     */

    @Transactional
    @Override
    synchronized public int audit(JySchoolResearchActivity activity) {
        JySchoolResearchActivity dbActivity = mapper.selectJySchoolResearchActivityById(activity.getId());
        Assert.isTrue(ProcessStatusEnum.AUDIT.isEqualsTo(dbActivity.getAuditState()), "当前状态不能审核");
       if(ProcessStatusEnum.REJECT.isEqualsTo(activity.getAuditState())){
           Assert.isTrue(StrUtil.isNotBlank(activity.getAuditMsg()), "驳回时审核信息不能为空");
       }

        JySchoolResearchActivity newActivity=new JySchoolResearchActivity();
        newActivity.setId(dbActivity.getId());
        newActivity.setAuditState(activity.getAuditState());
        newActivity.setAuditMsg(activity.getAuditMsg());

        newActivity.setUpdateBy(SecurityUtils.getUsername());
        newActivity.setUpdateTime(DateUtils.getNowDate());

        return mapper.updateJySchoolResearchActivity(newActivity);
    }


    void saveFileAndTeacherRela(JySchoolResearchActivity activity) {
        fileInfoService.saveOrUpdateFiles(activity.getId().toString(), BUSINESS_TYPE, CONTENT_SUBTYPE, activity.getContentFileList());
        fileInfoService.saveOrUpdateFiles(activity.getId().toString(), BUSINESS_TYPE, IMG_SUBTYPE, activity.getImgFileList());

        teacherService.insertActivityUser(activity.getId(), BUSINESS_TYPE, ActivityUserTypeEnum.ABSENCE.getCode(), activity.getAbsenceUserList());
        teacherService.insertActivityUser(activity.getId(), BUSINESS_TYPE, ActivityUserTypeEnum.LATE.getCode(), activity.getLateUserList());
        teacherService.insertActivityUser(activity.getId(), BUSINESS_TYPE, ActivityUserTypeEnum.LEAVE.getCode(), activity.getLeaveUserList());
    }
}
